package org.showminifier.web;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.showminifier.core.Options;

/**
 * @author mint
 *
 */
public class Test {
	public static String prettyName(String inp,boolean onlyLetter) {
		StringBuilder sb = new StringBuilder();
		int a = 'a', z = 'z', A = 'A', Z = 'Z';
		boolean to = true;
		for(int i = 0,len = inp.length(); i < len; i++){
			int ch = inp.charAt(i);
			if(to){
				if(ch >= a || ch <=z){
					ch = ch - 32;
					to = false;
				}else if(ch >= A || ch <=Z){
					to = false;
				}
			}
			if((ch < a || ch > z) && (ch < A || ch > Z))
				to = true;
			if(onlyLetter){
				sb.append(Character.isLetter((char)ch) ? Character.toString((char)ch) : "");
			}else{
				sb.append(Character.toString((char)ch));
			}
		}
		return sb.toString();
	}
	private static HashMap<String, Boolean> options = new HashMap<String, Boolean>();
	static {
		options.put("collapse-boolean-attributes",true);
		options.put("collapse-whitespace",true);
		options.put("remove-attribute-quotes",true);
		options.put("remove-comments-from-cdata",true);
		options.put("remove-comments",true);
		options.put("remove-cdata-sections-from-cdata",true);
		options.put("remove-empty-attributes",true);
		options.put("remove-empty-elements",false);
		options.put("remove-optional-tags",true);
		options.put("remove-redundant-attributes",true);
		options.put("remove-url-scheme",true);
		options.put("use-short-doctype",true);
		options.put("remove-script-type-attributes",false);
		options.put("remove-style-type-attributes",false);
	}
	public static boolean isDefaultOptions(HttpServletRequest req){
		for(String key :options.keySet()){
			String name = req.getParameter(key);
			boolean value = name == null ? false : name.equalsIgnoreCase("on");
			if(value != options.get(name)){
				return false;
			}
		}		
		return true;
	}
	private static HashMap map = new HashMap();
	public static void print(){
		Method[] init = Options.class.getDeclaredMethods();
		for(int i = 0 ,len = init.length; i< len ; i++){
			if(init[i].getName().startsWith("set")){
				//System.out.println(init[i].getName());
				map.put(init[i].getName(),init[i].getName());
			}
		}
	}
	public static Options buildOptions(HashMap req){
		Options option = new Options();
		int i = 0;
		for(String key : options.keySet()){
			String name =  req.containsKey(key) ? key : "";
			Method init = null;
		    try {
		      init = Options.class.getDeclaredMethod("set"+prettyName(name,true),new Class[]{boolean.class});
		      init.invoke(option,new Object[]{defaultOption(name, true)});
		    } catch (NoSuchMethodException ex) {
		    	System.out.println("set"+prettyName(name,true) + ":" +map.containsKey("set"+prettyName(name,true))+"\t" + (i++));
		    	ex.printStackTrace();
		    	//throw new RuntimeException(ex);
		    } catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(option.isRemoveComments());
		/*
		//Options.class.getDeclaredMethod(name,new Class[]{boolean.class});
        options.setCollapseBooleanAttributes(defaultOption(req.getParameter("collapse-boolean-attributes"), true) );
        options.setCollapseWhitespace(defaultOption(req.getParameter("collapse-whitespace"), true));						
		options.setRemoveAttributeQuotes(defaultOption(req.getParameter("remove-attribute-quotes"), true));		
		options.setRemoveCDATASectionsFromCDATA(defaultOption(req.getParameter("remove-comments-from-cdata"), true));		
		options.setRemoveComments(defaultOption(req.getParameter("remove-comments"), true));		
		options.setRemoveCommentsFromCDATA(defaultOption(req.getParameter("remove-cdata-sections-from-cdata"), true));	
		options.setRemoveEmptyAttributes(defaultOption(req.getParameter("remove-empty-attributes"), true));		
		options.setRemoveEmptyElements(defaultOption(req.getParameter("remove-empty-elements"), false));		
		options.setRemoveOptionalTags(defaultOption(req.getParameter("remove-optional-tags"), true));		
		options.setRemoveRedundantAttributes(defaultOption(req.getParameter("remove-redundant-attributes"), true));		
		options.setRemoveURLScheme(defaultOption(req.getParameter("remove-url-scheme"), true));		
		options.setUseShortDoctype(defaultOption(req.getParameter("use-short-doctype") , true));
		options.setRemoveScriptTypeAttributes(defaultOption(req.getParameter("remove-script-type-attributes"), false));
		options.setRemoveStyleTypeAttributes(defaultOption(req.getParameter("remove-style-type-attributes"), false));
		options.setEmbedLint(false);	
		*/
		return option;		
	}
	
	public static boolean defaultOption(String param,boolean defaultValue){		
		return param == null ? defaultValue : param.equalsIgnoreCase("on") ? true : false;
	}
	
	public static void main(String[] args){
		
		Test.print();
		Test.buildOptions(options);
	}
}
